package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.User;
import com.hzj.recipe.mapper.UserMapper;
import com.hzj.recipe.service.LoginService;
import com.hzj.recipe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 蜗牛菌
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2024-01-04 10:49:47
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    @Lazy
    private LoginService loginService;

    @Override
    public Object listAll(Integer index, Integer size, User user) {
        //默认第一页开始，每页5条
        if (index == null || index == 0) {
            index = 1;
        }
        if (size == null || size == 0) {
            size = 5;
        }
        Page<User> pageInfo = new Page(index, size);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (user.getUserId() != null && user.getUserId() != 0 && !Objects.equals(user.getUserId(), "")) {
            lambdaQueryWrapper.eq(User::getUserId, user.getUserId());
        }
        if (user.getUserOpenid() != null && !Objects.equals(user.getUserOpenid(), "")) {
            lambdaQueryWrapper.eq(User::getUserOpenid, user.getUserOpenid());
        }

        if ((user.getUserName() != null) && !Objects.equals(user.getUserName(), "")) {
            lambdaQueryWrapper.like(User::getUserName, user.getUserName());
        }
        if ((user.getUserPhone() != null) && !Objects.equals(user.getUserPhone(), "")) {
            lambdaQueryWrapper.like(User::getUserPhone, user.getUserPhone());
        }
        if ((user.getUserEmail() != null) && !Objects.equals(user.getUserEmail(), "")) {
            lambdaQueryWrapper.like(User::getUserEmail, user.getUserEmail());
        }
        Page<User> userPage = userMapper.selectPage(pageInfo, lambdaQueryWrapper);

        return userPage;
    }

    //验证用户名和密码
    @Override
    public User findUser(String userNum, String userpassword) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (userNum.contains("@")) {
            queryWrapper.eq(User::getUserEmail, userNum);
        } else {
            queryWrapper.eq(User::getUserPhone, userNum);
        }
        queryWrapper.eq(User::getUserPassword, userpassword);
        //查到一条符合就不再往下查找了
        queryWrapper.last("limit 1");

        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findPE(String userPhoneOrEmail) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (userPhoneOrEmail.contains("@")) {
            queryWrapper.eq(User::getUserEmail, userPhoneOrEmail);
        } else {
            queryWrapper.eq(User::getUserPhone, userPhoneOrEmail);
        }
        //查到一条符合就不再往下查找了
        queryWrapper.last("limit 1");

        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public R selectCurrentUser(String token) {
        User user = this.loginService.checkToken(token);
        //验证token是否合法
        if (user == null) {
            //验证失败，返回错误信息
            return R.failed("token不合法");
        }
        //验证成功，返回对应结果
        return R.ok(user);
    }
}




